package gang.com.mobelhelper.presenter;

import android.Manifest;
import android.app.Activity;

import com.tbruyelle.rxpermissions.RxPermissions;

import javax.inject.Inject;

import gang.com.mobelhelper.bean.AppInfo;
import gang.com.mobelhelper.bean.PageBean;
import gang.com.mobelhelper.common.rx.RxHttpRespCompat;
import gang.com.mobelhelper.common.rx.subscriber.ErrorHandlerSubscriber;
import gang.com.mobelhelper.common.rx.subscriber.ProgressDialogSubscriber;
import gang.com.mobelhelper.data.remote.RecommendModel;
import gang.com.mobelhelper.presenter.contract.RecommendContract;
import rx.functions.Action1;

/**
 * Created by xiaogangzai on 2017/7/12.
 * <p>
 * MVP中的P既要联系V又要联系M
 * RecommendPresenter实现Presenter,注意在P里面做逻辑处理
 */


public class RecommendPresenter extends BasePresenter<RecommendModel, RecommendContract.View> {

    //RecommendModel recommendModel;

    //首先要引用View，通过构造方法把View传进来
    //private RecommendContract.View mView;
    //子类继承父类的意义???????????????????????????????????????????????问题不懂
    @Inject
    public RecommendPresenter(RecommendModel recommendModel, RecommendContract.View view) {
        super(recommendModel, view);
    }

    /**
     * 权限检测的方法
     */
    public void request_pression() {
        RxPermissions rxPermissions = new RxPermissions((Activity) mContext);
        rxPermissions.request(Manifest.permission.READ_PHONE_STATE).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean aBoolean) {
                if (aBoolean) {
                    //授权成功
                    mView.Permission_Sucess();
                } else {
                    //授权失败
                    mView.Permission_fail();
                }
            }
        });
    }
    //直接在构造方法里面写Inject，它自己会去Moudle里面去找
    // @Inject
    /*public RecommendPresenter(RecommendContract.View mView, RecommendModel model) {
        this.mView = mView;
        recommendModel = model;
    }*/

    public void requestDatas() {
        mView.showLoading();
        mModel.getApps()
                .compose(RxHttpRespCompat.<PageBean<AppInfo>>compatResult())
                .subscribe(new ProgressDialogSubscriber<PageBean<AppInfo>>(mContext) {
                    @Override
                    public void onNext(PageBean<AppInfo> appInfoPageBean) {
                        //在这里获取数据之后就可以和前面构造方法传过来的View绑定
                        if (appInfoPageBean != null) {
                            //这里是调用View接口的方法
                            mView.showResult(appInfoPageBean.getDatas());
                        } else {
                            mView.showNodata();
                        }
                    }

                    @Override
                    public boolean isShowProgressDialog() {
                        return false;
                    }
                });
    }
}
